package com.tulingxueyuan.mall.modules.oms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.common.api.CommonPage;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.dto.OmsOrderViewDTO;
import com.tulingxueyuan.mall.dto.OrderConditionDTO;
import com.tulingxueyuan.mall.dto.OrderReceiverInfoDTO;
import com.tulingxueyuan.mall.dto.OrderUpdateStatusDTO;
import com.tulingxueyuan.mall.modules.oms.model.OmsOrder;
import com.tulingxueyuan.mall.modules.oms.service.OmsOrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author XuShu
 * @since 2023-05-28
 */
@RestController
@RequestMapping("/order")
public class OmsOrderController {
    @Resource
    private OmsOrderService omsOrderService;

    @GetMapping("/list")
    @ApiOperation(value = "订单分页")
    public CommonResult<CommonPage<OmsOrder>> list(OrderConditionDTO orderConditionDTO){
        Page page =  omsOrderService.listPage(orderConditionDTO);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "订单详情")
    public CommonResult<OmsOrderViewDTO> getById(@PathVariable Integer id){
        OmsOrderViewDTO omsOrderViewDTO = omsOrderService.getByIdWithOrderItem(id);
        return CommonResult.success(omsOrderViewDTO);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "批量删除订单")
    public CommonResult delete(@RequestParam(value = "ids") List<Integer> ids){
        Boolean b = omsOrderService.removeByIds(ids);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed();
        }
    }

    @PostMapping("/update/delivery")
    @ApiOperation(value = "批量订单发货")
    public CommonResult orderShipment(@RequestBody List<OrderUpdateStatusDTO> list){
        Boolean b = omsOrderService.updateShipment(list);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed();
        }
    }

    @PostMapping("/update/receiverInfo")
    @ApiOperation(value = "修改联系人信息")
    public CommonResult receiverInfo(@RequestBody OrderReceiverInfoDTO orderReceiverInfoDTO){
        Boolean b = omsOrderService.updateSReceiver(orderReceiverInfoDTO);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed();
        }
    }

    @PostMapping("/update/note")
    @ApiOperation(value = "修改订单备注")
    public CommonResult updateNote(OmsOrder omsOrder){
        Boolean b = omsOrderService.updateNote(omsOrder);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed();
        }
    }

    @PostMapping("/update/close")
    @ApiOperation(value = "批量关闭订单")
    public CommonResult closeOrder(@RequestParam(value = "ids") List<Long> ids, String note){
        Boolean b = omsOrderService.closeOrder(ids,note);
        if (b){
            return CommonResult.success(b);
        }else {
            return CommonResult.failed();
        }
    }

}

